# define _CRT_SECURE_NO_WARNINGS
#include"binarytree.h"


BTNode* BTreeCreate(BTDataType* a, int* pi)
{
	if (a[*pi] == '#')
	{
		(*pi)++;
		return NULL;
	}
	BTNode* root = (BTNode*)malloc(sizeof(BTNode));
	if (root == NULL)
	{
		perror("BTreeCreate::malloc");
		return NULL;
	}
	root->val = a[(*pi)++];
	root->left = BTreeCreate(a, pi);
	root->right = BTreeCreate(a, pi);

	return root;
}

void BTreeDestory(BTNode* root)
{
	if (root == NULL)
<<<<<<< HEAD
		return;
	BTreeDestory(root->left);
	BTreeDestory(root->right);
	free(root);
=======
	    return;
    BTreeDestory(root->left);
    BTreeDestory(root->right);
    free(root);
>>>>>>> 37d5066f35bd383a616a32e579329f5c36bbff85
}

int BTreeSize(BTNode* root)
{
	if (root == NULL)
		return 0;
	return BTreeSize(root->left) + BTreeSize(root->right) + 1;
}

int BTLeafSize(BTNode* root)
{
	if (root == NULL)
		return 0;
	if (root->left == NULL && root->right == NULL)
		return 1;
	return BTLeafSize(root->left) + BTLeafSize(root->right);
}

int BTLevelKSize(BTNode* root, int k)
{
	assert(k > 0);

	if (root == NULL)
		return 0;
	if (k == 1)
		return 1;
	return BTLevelKSize(root->left, k - 1) + BTLevelKSize(root->right, k - 1);
}

BTNode* BTreeFind(BTNode* root, BTDataType x)
{
	if (root == NULL)
		return NULL;
	if (root->val == x)
	{
		return root;
	}
	BTNode* left = BTreeFind(root->left, x);
	if (left)
		return left;
	BTNode* right = BTreeFind(root->right, x);
	if (right)
		return right;
	return NULL;
}

void PrevOrder(BTNode* root)
{
	if (root == NULL)
		return;
	printf("%c ", root->val);
	PrevOrder(root->left);
	PrevOrder(root->right);
}

void InOrder(BTNode* root)
{
	if (root == NULL)
		return;
	InOrder(root->left);
	printf("%c ", root->val);
	InOrder(root->right);
}

void PostOrder(BTNode* root)
{
	if (root == NULL)
		return;
	PostOrder(root->left);
	PostOrder(root->right);
	printf("%c ", root->val);
}

void BTLevelOrder(BTNode* root)
{
	Queue q;
	QInit(&q);
	if (root)
		QPush(&q, root);
	while (!QEmpty(&q))
	{
		BTNode* front = QFront(&q);
		QPop(&q);
		printf("%c ", front->val);
		if (front->left)
			QPush(&q, front->left);
		if (front->right)
			QPush(&q, front->right);
	}

	QDestroy(&q);
}

int BTComplete(BTNode* root)
{
<<<<<<< HEAD
	Queue q;
	QInit(&q);
	if (root)
		QPush(&q, root);
	while (!QEmpty(&q))
	{
		BTNode* front = QFront(&q);
		QPop(&q);
		if (front == NULL)
			break;
		QPush(&q, front->left);
		QPush(&q, front->right);
	}
	while (!QEmpty(&q))
	{
		BTNode* front = QFront(&q);
		QPop(&q);
		if (front)
		{
			QDestroy(&q);
			return false;
		}
	}
	QDestroy(&q);
	return true;
=======
    Queue q;
    QInit(&q);
    if (root)
	QPush(&q, root);
    while (!QEmpty(&q))
    {
	    BTNode* front = QFront(&q);
	    QPop(&q);
	    if (front == NULL)
	    	break;
	    QPush(&q, front->left);
	    QPush(&q, front->right);
    }
    while (!QEmpty(&q))
    {
	    BTNode* front = QFront(&q);
	    QPop(&q);
	    if (front)
	    {
	    	QDestroy(&q);
	    	return false;
	    }
    }
    QDestroy(&q);
    return true;
>>>>>>> 37d5066f35bd383a616a32e579329f5c36bbff85
}




BTNode* CreatTree()
{
	BTNode* node1 = BuyNode(1);
	BTNode* node2 = BuyNode(2);
	BTNode* node3 = BuyNode(3);
	BTNode* node4 = BuyNode(4);
	BTNode* node5 = BuyNode(5);
	BTNode* node6 = BuyNode(6);
	BTNode* node7 = BuyNode(7);

	node1->left = node2;
	node1->right = node4;
	node2->left = node3;
	node4->left = node5;
	node4->right = node6;
	node3->right = node7;

	return node1;
}

BTNode* BuyNode(BTDataType x)
{
	BTNode* node = (BTNode*)malloc(sizeof(BTNode));
	if (node == NULL)
	{
		perror("BuyNode::malloc");
		return NULL;
	}
	node->val = x;
	node->left = NULL;
	node->right = NULL;
	return node;
}

